package com.itggm.controller;

import com.itggm.Result.ResponseResult;
import com.itggm.dto.StudentPageDTO;
import com.itggm.exception.BusinessException;
import com.itggm.pojo.Student;
import com.itggm.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

/**
 * @author: ggm
 * @data 2024年12月27日 16:47
 */
@Slf4j
@RestController
@RequestMapping("/student")
public class StudentController {
    @Resource
    private StudentService studentService;

     /**
      * @Author: ggm
      * @date:  2024/12/27
      * 学生分页列表
      */
     @PostMapping("/page")
     public ResponseResult studentList(@RequestBody StudentPageDTO studentPageDTO){
         log.info("学生分页列表{}",studentPageDTO);
         return studentService.studentList(studentPageDTO);
     }
      /**
       * @Author: ggm
       * @date:  2024/12/27
       * 添加或修改学生信息
       */
      @PostMapping("/addOrUpdate")
      public ResponseResult addOrUpdateStudent(@RequestBody Student student){
          log.info("添加或修改学生信息{}",student);
          try {
              studentService.saveOrUpdate(student);
              return  ResponseResult.success();
          }catch (DuplicateKeyException e){
              throw  new BusinessException("学号"+student.getStudentNo()+"重复");
          }
      }
       /**
        * @Author: ggm
        * @date:  2024/12/28
        * 删除学生信息
        */
       @GetMapping("/delete/{id}")
       public ResponseResult deleteStudent(@PathVariable("id") Long id){
           log.info("删除学生信息{}", id);
           studentService.removeById(id);
           return ResponseResult.success();
       }
        /**
         * @Author: ggm
         * @date:  2024/12/28
         * 批量导入文件
         */
        @PostMapping("import")
        public ResponseResult importStudent(@RequestParam("file") MultipartFile file){
            log.info("批量导入文件{}",file);
            return  studentService.importStudent(file);
        }
         /**
          * @Author: ggm
          * @date:  2024/12/30
          * 导出文件
          */
         @GetMapping("export")
         public void exportStudents(String studentNo, String clazz, HttpServletResponse response){
             log.info("导出文件{}",studentNo);
             studentService.exportStudent(studentNo,clazz,response);
         }




}
